package za.org.jcicada.genss.alarm.impl;

/**
 * AlarmSubjectProxy.java
 *
 * @author Simon Cross
 *
 * Created on February 11, 2009, 12:32
 */

import java.net.URI;

import za.org.jcicada.comms.IConnector;
import za.org.jcicada.comms.impl.MessageListenerBase;
import za.org.jcicada.comms.impl.ProxyBase;
import za.org.jcicada.genss.alarm.AlarmURN;
import za.org.jcicada.genss.alarm.IAlarmObserver;
import za.org.jcicada.genss.alarm.IAlarmSubject;
import za.org.jcicada.genss.alarm.messages.AcknowledgeAlarmReply;
import za.org.jcicada.genss.alarm.messages.AcknowledgeAlarmRequest;
import za.org.jcicada.genss.alarm.messages.AlarmInform;
import za.org.jcicada.genss.alarm.messages.AlarmSetReply;
import za.org.jcicada.genss.alarm.messages.AlarmSetRequest;
import za.org.jcicada.genss.alarm.messages.AttachReply;
import za.org.jcicada.genss.alarm.messages.AttachRequest;
import za.org.jcicada.genss.alarm.messages.DetachReply;
import za.org.jcicada.genss.alarm.messages.DetachRequest;
import za.org.jcicada.genss.alarm.messages.ReportReply;
import za.org.jcicada.genss.alarm.messages.ReportRequest;
import za.org.jcicada.genss.messages.TerminateServiceInform;

/**
 * IAlarmSubject implementation that proxies requests through to
 * a remote alarm subject.
 */
public class AlarmSubjectProxy extends ProxyBase<IAlarmObserver> implements IAlarmSubject {

    /**
     * Create an AlarmSubjectProxy.
     *
     * @param connector IConnector object for creating connections to the remote subject.
     */
    public AlarmSubjectProxy(IConnector connector) {
        super(connector);
    }
    
    /**
     * Create an AlarmSubjectProxy.
     * 
     * @param locationURI address of the acceptor
     */
    public AlarmSubjectProxy(URI locationURI) {
        super(locationURI);
    }

    @Override
    protected MessageListenerBase createReceiver(IAlarmObserver client) {
        return new Receiver(client);
    }
    
    // IAlarmSubject methods

    /**
     * Send AttachRequest.
     */
    public void requestAttach(IAlarmObserver observer) {
        sendMessage(observer, new AttachRequest());
    }

    /**
     * Send DetachRequest.
     */
    public void requestDetach(IAlarmObserver observer) {
        sendMessage(observer, new DetachRequest());
    }

    /**
     * Send AlarmSetRequest.
     */
    public void requestAlarmSet(IAlarmObserver observer) {
        sendMessage(observer, new AlarmSetRequest());
    }

    /**
     * Send ReportRequest.
     */
    public void requestReport(IAlarmObserver observer) {
        sendMessage(observer, new ReportRequest());
    }

    /**
     * Send AcknowledgeAlarmRequest.
     */
    public void requestAcknowledgeAlarm(IAlarmObserver observer, AlarmURN alarmURN) {
        sendMessage(observer, new AcknowledgeAlarmRequest(alarmURN));
    }
    
    /**
     * Implementation of IMessageListener that dispatches messages to an IAlarmObserver.
     */
    public class Receiver extends MessageListenerBase {

        /**
         * Handle to callback object for messages from the subject to the observer.
         */
        IAlarmObserver observer;

        public Receiver(IAlarmObserver observer) {
            this.observer = observer;
        }

        public void onClose() throws Exception {
            observer.informDisconnect();
        }

        // dispatch methods

        /**
         * Dispatch AttachReply.
         */
        public void alarmAttachReply(AttachReply msg) {
            observer.replyAttach(msg.getResult());
        }

        /**
         * Dispatch DetachReply.
         */
        public void alarmDetachReply(DetachReply msg) {
            observer.replyDetach(msg.getResult());
        }

        /**
         * Dispatch AlarmSetReply.
         */
        public void alarmAlarmSetReply(AlarmSetReply msg) {
            observer.replyAlarmSet(msg.getResult(), msg.getAlarmURNs());
        }

        /**
         * Dispatch ReportReply.
         */
        public void alarmReportReply(ReportReply msg) {
            observer.replyReport(msg.getResult(), msg.getReport());
        }

        /**
         * Dispatch AcknowledgeAlarmReply.
         */
        public void alarmAcknowledgeAlarmReply(AcknowledgeAlarmReply msg) {
            observer.replyAcknowledgeAlarm(msg.getResult(), msg.getAlarmURN());
        }

        // inform messages

        /**
         * Dispatch AlarmInform.
         */
        public void alarmAlarmInform(AlarmInform msg) {
            observer.informAlarm(msg.getAlarmURN(), msg.getAlarm());
        }

        /**
         * Dispatch TerminateServiceInform.
         */
        public void alarmTerminateServiceInform(TerminateServiceInform msg) {
            observer.informTerminateService((AlarmURN)msg.getServiceURN());
        }
    }

}
